//
//  ViewController.m
//  07-滚动大图
//
//  Created by wangneihan on 15/12/15.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) UIScrollView *scrollView;

@property (nonatomic,weak) UIImageView *imageView;

-(IBAction)btnClick;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建 UIScrollView
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    scrollView.backgroundColor = [UIColor grayColor];
    
    scrollView.showsHorizontalScrollIndicator = NO;
    
    scrollView.showsVerticalScrollIndicator = NO;
    
    //设置内边距
    scrollView.contentInset = UIEdgeInsetsMake(50, 50, 50, 50);
    //弹簧效果
    scrollView.bounces = NO;
    //设置内容的偏移
//    scrollView.contentOffset = CGPointMake(-50, -50);
    
    [self.view addSubview:scrollView];
    
    //创建 UIImageView
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"IMG_2780"]];
    
    CGFloat imgH = imageView.image.size.height;
    
    CGFloat imgW = imageView.image.size.width;
    
    imageView.frame = CGRectMake(0, 0, imgW, imgH);
    
    [scrollView addSubview:imageView];
    
    scrollView.contentSize = imageView.image.size;
    
    UIButton *run = [[UIButton alloc] initWithFrame:CGRectMake(150, 150, 100, 50)];
    
    run.backgroundColor = [UIColor redColor];
    
    [run setTitle:@"偏移" forState:UIControlStateNormal];
    
    [run addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [scrollView addSubview:run];
    
    scrollView.contentOffset = CGPointMake(50, 50);
    
    _scrollView = scrollView;
    
    _imageView = imageView;
    
    _scrollView.delegate = self;
    
    _scrollView.maximumZoomScale = 4 ;
    
    _scrollView.minimumZoomScale = 0.1;
    
    
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
        return _imageView;
}

-(IBAction)btnClick{
    CGPoint offset =  self.scrollView.contentOffset;
    
    offset.x += 50;
    
    [UIView animateWithDuration:0.5 animations:^{
        //动画的方式设置偏移
        //[self.scrollView setContentOffset:offset animated:YES];
        self.scrollView.contentOffset = offset;
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
